1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #include<stdio.h> #include<stdlib.h>
void print_route(int *l1,int *l2,int lfinal,int n); int Fastest_way(int *a1,int n1,int *a2,int n2,int *t12,int n3,int *t21,int n4,int e1,int e2,int x1,int x2,int *l1,int *l2,int lfinal );
void main() { int a1[]={7,9,3,4,8,4}; int a2[]={8,5,6,4,5,7}; int t12[]={2,3,1,3,4}; int t21[]={2,1,2,2,1}; int e1=2; int e2=4; int x1=3; int x2=2; int n1=sizeof(a1)/sizeof(int); int n2=sizeof(a2)/sizeof(int); int n3=sizeof(t12)/sizeof(int); int n4=sizeof(t21)/sizeof(int); int l1[6]={0}; int l2[6]={0}; int lfinal=0; lfinal=Fastest_way(a1,n1,a2,n2,t12,n3,t21,n4,e1,e2,x1,x2,l1,l2,lfinal ); print_route(l1,l2,lfinal,n1);
}
int Fastest_way(int *a1,int n1,int *a2,int n2,int *t12,int n3,int *t21,int n4,int e1,int e2,int x1,int x2,int *l1,int *l2,int lfinal ) {
int f1[6]={0}; int f2[6]={0}; int final=0; f1[0]=e1+a1[0]; f2[0]=e2+a2[0];
for(int i=1;i<n1;i++) { if((f1[i-1]+a1[i])<=(f2[i-1]+a1[i]+t21[i-1])) { f1[i]=f1[i-1]+a1[i]; l1[i]=1; } else { f1[i]=f2[i-1]+a1[i]+t21[i-1]; l1[i]=2; }
if((f2[i-1]+a2[i])<=(f1[i-1]+a2[i]+t12[i-1])) { f2[i]=f2[i-1]+a2[i]; l2[i]=2; } else { f2[i]=f1[i-1]+a2[i]+t12[i-1]; l2[i]=1; } }
if((f1[n1-1]+x1)<(f2[n1-1]+x2)) { final=f1[n1-1]+x1; lfinal=1; } else { final=f2[n1-1]+x2; lfinal=2; }
return lfinal; }
void print_route(int *l1,int *l2,int lfinal,int n) { int flag=0; printf("line: %d ,station: %d\n",lfinal,n); for(int i=n-1;i>0;i--) { if(i==n-1) { if(lfinal==1) { printf("line: %d ,station: %d\n",l1[i],i); if(l1[i]==1) flag=1; else flag=2; } else { printf("line: %d ,station: %d\n",l2[i],i); if(l2[i]==1) flag=1; else flag=2; } } else { if(flag==1) { printf("line: %d ,station: %d\n",l1[i],i); if(l1[i]==1) flag=1; else flag=2; } else { printf("line: %d ,station: %d\n",l2[i],i); if(l2[i]==1) flag=1; else flag=2; } }
} }
|